import React, { useState, useContext, useEffect } from 'react'
import CommonTable from '@/common/CommonTable'
import { tableConfig, dataSource } from './config'
import { TableChildrenTableContext } from '@/config/globalContext'

const ChildrenTable = () => {
  const [selectedRowKeys, setSelectedRowKeys] = useState([])
  const { allButton } = useContext(TableChildrenTableContext)
  useEffect(() => {
    if (allButton) {
      setSelectedRowKeys(
        _.map(dataSource, (item, key) => {
          return item.id
        })
      )
    } else {
      setSelectedRowKeys([])
    }
  }, [allButton])

  const tableProps = {
    columns: tableConfig,
    dataSource: dataSource,
    rowKey: 'id',
    rowSelection: {
      selectedRowKeys,
      onChange: (val, row) => {
        setSelectedRowKeys(val)
      },
    },
  }
  return (
    <div>
      <CommonTable {...tableProps} />
    </div>
  )
}
export default ChildrenTable
